<?php

namespace app\admin\controller;

use wechat\src\WeChat;

/**
 * 微信红包二维码，
 */
class Wechatredbao extends base
{

    protected $wechat;

    public function _initialize()
    {
        $this->wechat = (new WeChat);
        $this->wechat->valid();

    }

    /**
     * 红包列表
     */
    public function redbao()
    {
        $where = array();
        $getdata = $this->request->param();
        if (isset($getdata['state'])) {
            $where['state'] = $getdata['state'];
        }
        $data = db('redbao')->order('id DESC')->where($where)->paginate(3);
        $this->assign('data', $data);
        return $this->fetch();

    }

    /**
     * 查看红包二维码
     */
    public function  checkredbao(){
        $data = db('redbao')->field('qrcode_url')->order('id DESC')->where('state',0)->paginate();

        $this->assign('data', $data);
        return $this->fetch();
    }

    /**
     * 增加二维码
     *
     * @return \think\response\View
     */
    public function redbaoadd()
    {

        if ($this->request->isPost()) {
            $qrModel = db('redbao');
            $IN = input();

            $data['title'] = $IN['title'];
            $data['subtitle'] = $IN['subtitle'];
            $data['money'] = $IN['money'];
            $data['textmessage'] = $IN['textmessage'];
            $data['state'] = 0;
            $data['qr_type'] = $IN['qr_type'];
            $data['scene_str'] = $IN['scene_str'];
            $data['create_time'] = time();
            $data['scene_id'] = mt_rand(1, 1000000);
            if ($data['qr_type'] == 0) {//临时二维码
                $data['expire'] = $IN['time'];
                if ($data['expire'] == '') {
                    $data['expire'] = 1800;
                }

                for ($i = 0; $i < $IN['num']; $i++) {

                    $result = $this->wechat->instance('Qrcode')->createQrcode($data['scene_id'], $data['qr_type'], $data['expire']);
                    $data['ticket'] = $result['ticket'];
                    $data['qrcode_url'] = $this->wechat->instance('Qrcode')->getQrcode($result['ticket']);
//                    $data['short_url'] = $this->wechat->instance('Qrcode')->getQrshortUrl($data['qrcode_url']);
                    $data['expire'] = $result['expire_seconds'];
                    $data['url'] = $result['url'];
//                    if ($data['short_url'] == '') {
//                        ajaxMsg('0', 'ErrMsg: 生成二维码短连接失败');
//                    }
                    if ($qrModel->where(['scene_id' => $data['scene_id']])->find()) {
                        $data['scene_id'] = mt_rand(1, 1000000);
                    }

                    $addredbao[$i] =
                        [
                            'title' => $data['title'],
                            'subtitle' => $data['subtitle'],
                            'money' => $data['money'],
                            'textmessage' => $data['textmessage'],
                            'state' => $data['state'],
                            'qr_type' => $data['qr_type'],
                            'scene_str' => $data['scene_str'],
                            'create_time' => $data['create_time'],
                            'scene_id' => mt_rand(1, 1000000),
                            'ticket' => $data['ticket'],
                            'qrcode_url' => $data['qrcode_url'],
//                            'short_url' => $data['short_url'],
                        'expire' => $data['expire'],
                        'url' => $data['url'],
                    ];
                }
                if ($qrModel->insertAll($addredbao)) {
                    ajaxReturn(['url' => url('redbao', ['type' => 'list'])], 1, '增加二维码成功');
                }

            }
            if ($data['qr_type'] == 2) {//永久二维码
                if (!$data['scene_str']) {
                    $data['scene_str'] = $data['scene_id'];
                }
                if (strlen($data['scene_str']) > 64) {
                    ajaxMsg('0', 'ErrMsg: 场景值字符小于64个字符');
                }

                for ($i = 0; $i < $IN['num']; $i++) {

                    $result = $this->wechat->instance('Qrcode')->createQrcode($data['scene_id'], $data['qr_type']);
                    $data['ticket'] = $result['ticket'];
                    $data['qrcode_url'] = $this->wechat->instance('Qrcode')->getQrcode($result['ticket']);
//                    $data['short_url'] = $this->wechat->instance('Qrcode')->getQrshortUrl($data['qrcode_url']);
                    $data['url'] = $result['url'];
//                    if ($data['short_url'] == '') {
//                        ajaxMsg('0', 'ErrMsg: 生成二维码短连接失败');
//                    }
                    if ($qrModel->where(['scene_id' => $data['scene_id']])->find()) {
                        $data['scene_id'] = mt_rand(1, 1000000);
                    }

                    $addredbao[$i] =
                        [
                            'title' => $data['title'],
                            'subtitle' => $data['subtitle'],
                            'money' => $data['money'],
                            'textmessage' => $data['textmessage'],
                            'state' => $data['state'],
                            'qr_type' => $data['qr_type'],
                            'scene_str' => $data['scene_str'],
                            'create_time' => $data['create_time'],
                            'scene_id' => mt_rand(1, 1000000),
                            'ticket' => $data['ticket'],
                            'qrcode_url' => $data['qrcode_url'],
//                            'short_url' => $data['short_url'],
                        'url' => $data['url'],
                    ];
                }

                if ($qrModel->insertAll($addredbao)) {
                    ajaxReturn(['url' => url('redbao', ['type' => 'list'])], 1, '增加二维码成功');
                }

            }

        } else {
            return $this->fetch();
        }
    }

    public function delredbao($id)
    {
        if ($this->request->isAjax()) {
            db('redbao')->where('id',$id)->delete();
            ajaxMsg('1', '删除成功');
        }
    }
}
